﻿using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace WpfApp4
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private double textBox1Text;
        private double textBox2Text;
        private double num;

        public double TextBox1Text
        {
            get { return textBox1Text; }
            set
            {
                if (textBox1Text != value)//value用于表示调用者传递给属性的新值
                {
                    textBox1Text = value;
                    OnPropertyChanged();
                    UpdateNum();
                }
            }
        }

        public double TextBox2Text
        {
            get { return textBox2Text; }
            set
            {
                if (textBox2Text != value)
                {
                    textBox2Text = value;
                    OnPropertyChanged();
                    UpdateNum();
                }
            }
        }

        public double Num
        {
            get { return num; }
            set
            {
                if (num != value)
                {
                    num = value;
                    OnPropertyChanged();
                }
            }
        }
        
        private void UpdateNum()
        {
            double num1 = TextBox1Text;
            double num2 = TextBox2Text;

            Num = num1 + num2;
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}